/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.domain.IsfdLayupEquivalentMaterial;
import com.comac.ins.isfd.domain.bo.IsfdLayupEquivalentMaterialBo;
import com.comac.ins.isfd.domain.vo.IsfdLayupEquivalentMaterialVo;
import com.comac.ins.isfd.domain.vo.IsfdLayupMetalCaseMetaVo;
import com.comac.ins.isfd.domain.vo.IsfdMetalMaterialVo;
import com.comac.ins.isfd.mapper.IsfdLayupEquivalentMaterialMapper;
import com.comac.ins.isfd.service.IIsfdLayupEquivalentMaterialService;
import com.comac.ins.isfd.service.IIsfdLayupMetalCaseMetaService;
import com.comac.ins.isfd.service.IIsfdMetalMaterialService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2024-08-02
 */
@RequiredArgsConstructor
@Service
public class IsfdLayupEquivalentMaterialServiceImpl implements IIsfdLayupEquivalentMaterialService {

    @Autowired
    private IsfdLayupEquivalentMaterialMapper baseMapper;

    @Autowired
    protected IIsfdLayupMetalCaseMetaService iIsfdLayupMetalCaseMetaService;

    @Autowired
    protected IIsfdMetalMaterialService iIsfdMetalMaterialService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 主键
     * @return 【请填写功能名称】
     */
    @Override
    public IsfdLayupEquivalentMaterialVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询【请填写功能名称】列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 【请填写功能名称】分页列表
     */
    @Override
    public TableDataInfo<IsfdLayupEquivalentMaterialVo> queryPageList(IsfdLayupEquivalentMaterialBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IsfdLayupEquivalentMaterial> lqw = buildQueryWrapper(bo);
        Page<IsfdLayupEquivalentMaterialVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的【请填写功能名称】列表
     *
     * @param bo 查询条件
     * @return 【请填写功能名称】列表
     */
    @Override
    public List<IsfdLayupEquivalentMaterialVo> queryList(IsfdLayupEquivalentMaterialBo bo) {
        LambdaQueryWrapper<IsfdLayupEquivalentMaterial> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdLayupEquivalentMaterial> buildQueryWrapper(IsfdLayupEquivalentMaterialBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IsfdLayupEquivalentMaterial> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPlyCode()), IsfdLayupEquivalentMaterial::getPlyCode, bo.getPlyCode());
        lqw.eq(bo.getExx() != null, IsfdLayupEquivalentMaterial::getExx, bo.getExx());
        lqw.eq(bo.getEyy() != null, IsfdLayupEquivalentMaterial::getEyy, bo.getEyy());
        lqw.eq(bo.getGxy() != null, IsfdLayupEquivalentMaterial::getGxy, bo.getGxy());
        lqw.eq(bo.getT() != null, IsfdLayupEquivalentMaterial::getT, bo.getT());
        lqw.eq(bo.getPcompNumber() != null, IsfdLayupEquivalentMaterial::getPcompNumber, bo.getPcompNumber());
        lqw.eq(bo.getRho() != null, IsfdLayupEquivalentMaterial::getRho, bo.getRho());
        lqw.eq(bo.getX() != null, IsfdLayupEquivalentMaterial::getX, bo.getX());
        lqw.eq(bo.getY() != null, IsfdLayupEquivalentMaterial::getY, bo.getY());
        lqw.eq(bo.getXy() != null, IsfdLayupEquivalentMaterial::getXy, bo.getXy());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param bo 【请填写功能名称】
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(IsfdLayupEquivalentMaterialBo bo) {
        IsfdLayupEquivalentMaterial add = MapstructUtils.convert(bo, IsfdLayupEquivalentMaterial.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param bo 【请填写功能名称】
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(IsfdLayupEquivalentMaterialBo bo) {
        IsfdLayupEquivalentMaterial update = MapstructUtils.convert(bo, IsfdLayupEquivalentMaterial.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IsfdLayupEquivalentMaterial entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除【请填写功能名称】信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public IsfdLayupEquivalentMaterialVo queryByLayCode(String layCode) {
        IsfdLayupEquivalentMaterialVo isfdLayupEquivalentMaterialVo = baseMapper.selectVoOne(new LambdaQueryWrapper<IsfdLayupEquivalentMaterial>().eq(IsfdLayupEquivalentMaterial::getPlyCode, layCode));
        if (isfdLayupEquivalentMaterialVo != null) {
            return isfdLayupEquivalentMaterialVo;
        }
        IsfdLayupMetalCaseMetaVo isfdLayupMetalCaseMetaVo = iIsfdLayupMetalCaseMetaService.queryByLayCode(layCode);
        if (isfdLayupMetalCaseMetaVo == null) {
            throw new BaseException("铺层不存在，铺层编码：" + layCode + "没有查询到相关数据，请在铺层库确认数据后重试！");
        }
        String materialCode = isfdLayupMetalCaseMetaVo.getMaterialCode();
        IsfdMetalMaterialVo isfdMetalMaterialVo = iIsfdMetalMaterialService.queryByMaterialNumber(materialCode);
        if (isfdMetalMaterialVo == null) {
            throw new BaseException("金属材料不存在，材料编码：" + materialCode + "没有查询到相关数据，请在铺层库的材料中确认数据后重试！");
        }
        IsfdLayupEquivalentMaterialVo targetVo = new IsfdLayupEquivalentMaterialVo();
        targetVo.setPlyCode(layCode);
        targetVo.setExx(isfdLayupMetalCaseMetaVo.getExx());
        targetVo.setEyy(isfdLayupMetalCaseMetaVo.getEyy());
        targetVo.setGxy(isfdLayupMetalCaseMetaVo.getGxy());
        targetVo.setT(isfdLayupMetalCaseMetaVo.getThickness());
        targetVo.setPcompNumber(isfdLayupMetalCaseMetaVo.getPshellNumber());
        targetVo.setRho(isfdMetalMaterialVo.getRho());
        targetVo.setX(isfdMetalMaterialVo.getThmCooe());
        return targetVo;
    }
}
